package com.ykh.mybbsdemo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ykh.mybbsdemo.dao.mapper.CommentMapper;
import com.ykh.mybbsdemo.dao.pojo.po.Comment;
import com.ykh.mybbsdemo.dao.pojo.po.SysUser;
import com.ykh.mybbsdemo.service.CommentsService;
import com.ykh.mybbsdemo.service.SysUserService;
import com.ykh.mybbsdemo.util.UserThreadLocal;
import com.ykh.mybbsdemo.dao.pojo.vo.CommentVo;
import com.ykh.mybbsdemo.dao.pojo.vo.Result;
import com.ykh.mybbsdemo.dao.pojo.vo.UserVo;
import com.ykh.mybbsdemo.dao.pojo.dto.CommentParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-09-01
 * Time: 17:04
 */
@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private SysUserService userService;

    @Override
    public Result commentsByArticleId(Long articleId) {
        /**
         * 1. 根据文章ID查询评论列表
         * 2. 根据评论人员的ID查询评论人员的信息
         * 3. 判断如果 level = 1 要去查询它有没有子评论
         * 4. 如果有 根据评论ID进行查询(parent_id)
         */
        //1. 根据文章ID查询评论列表,先查第一级评论
        //构建查询语句
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Comment::getArticleId,articleId);
        queryWrapper.eq(Comment::getLevel,1);
        //进行查询
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        //转换成VO对象
        List<CommentVo> commentVoList = copyList(comments);
        return Result.success(commentVoList);
    }

    /**
     * 评论功能实现
     * @param commentParam
     * @return
     */
    @Override
    public Result comment(CommentParam commentParam) {
        //直接在控制层获取当前登录的用户对象
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        //给当前的评论赋值属性
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParam.getContent());
        comment.setParentId(comment.getParentId());
        comment.setCreateDate(LocalDateTime.now());
        //判断有无 父评论
        Long parent = commentParam.getParent();
        //没有,设置评论等级为1,有,设置评论等级为2
        if( parent == null || parent == 0) {
            comment.setLevel(1);
        } else {
            comment.setLevel(2);
        }
        comment.setParentId( parent == null ? 0 : parent);
        //判断有无子评论
        Long toUserId = commentParam.getToUserId();
        comment.setToUid( toUserId == null ? 0 : toUserId);
        //将评论插入评论列表
        this.commentMapper.insert(comment);
        CommentVo commentVo = copy(comment);
        return Result.success(commentVo);
    }

    private List<CommentVo> copyList(List<Comment> comments) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : comments) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        //转化成VO对象
        CommentVo commentVo = new CommentVo();
        commentVo.setId( String.valueOf(comment.getId()));
        BeanUtils.copyProperties(comment,commentVo);
        //2. 根据评论人员的ID查询评论人员的信息
        Long authorId = comment.getAuthorId();
        UserVo userVo = this.userService.findUserVoById(authorId);
        commentVo.setAuthor(userVo);
        //3. 判断如果 level = 1 要去查询它有没有子评论
        Integer level = comment.getLevel();
        if(1 == level) {
            Long id = comment.getId();
            //根据父评论id查询子评论
            List<CommentVo> commentVoList = findCommentsByParentId(id);
            //将子评论 加到父评论中
            commentVo.setChildrens(commentVoList);
        }
        //如果是二级[评论 ,to User 看下给谁评论
        if( level > 1) {
            //拿到当前评论的id
            Long toUid = comment.getToUid();
            //根据当前子评论的id,查找父评论的用户
            UserVo toUserVo = this.userService.findUserVoById(toUid);
            //找到后放到返回的Vo对象中
            commentVo.setToUser(toUserVo);
        }
        return commentVo;
    }

    //根据父评论ID 查询子评论
    private List<CommentVo> findCommentsByParentId(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId,id);
        //加个等级的限定条件,使结果更精确
        queryWrapper.eq(Comment::getLevel,2);
        return copyList(commentMapper.selectList(queryWrapper));
    }
}
